package com.ef.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.ef.constant.EUserTodoObject;
import com.ef.constant.EUserTodoStatus;
import com.ef.constant.EUserTodoType;

/**
 * 
 * @author CYG create:2009-12-26 上午11:47:40
 */
@Entity
public class UserTodoEvent implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	/**
	 * 任务的类型
	 */
	@Enumerated(value = EnumType.STRING)
	private EUserTodoType todoType;

	/**
	 * 任务的状态
	 */
	@Enumerated(value = EnumType.STRING)
	private EUserTodoStatus status;

	@Temporal(TemporalType.TIMESTAMP)
	private Date finishTime;

	@ManyToOne(fetch = FetchType.EAGER, optional = true)
	private User finishUser;

	/**
	 * 任务的对象类型
	 */
	@Enumerated(value = EnumType.STRING)
	private EUserTodoObject toObject;
	/**
	 * 产生人
	 */
	@ManyToOne(fetch = FetchType.EAGER, optional = true)
	private User createUser;

	/**
	 * 产生的日期
	 */
	@Temporal(TemporalType.TIMESTAMP)
	private Date createTime;
	
	/**
	 * 开始提醒日期
	 */
	@Temporal(TemporalType.TIMESTAMP)
	private Date reminderStartTime;

	/**
	 * 结束提醒日期
	 */
	@Temporal(TemporalType.TIMESTAMP)
	private Date reminderEndTime;

	/**
	 * 具体内容
	 */
	@Column(length = 500)
	private String detail;

	/**
	 * 具体内容
	 */
	@Column(length = 100)
	private String title;

	/**
	 * 操作Action
	 */
	@Column(length = 400)
	private String goAction;

	/**
	 * 指定到部门
	 */
	@ManyToOne(fetch = FetchType.EAGER, optional = true)
	private Department toDepartment;
	@Transient
	private Long toDepartmentId;
	/**
	 * 指定到部门
	 */
	@ManyToOne(fetch = FetchType.EAGER, optional = true)
	private Team toTeam;
	@Transient
	private Long toTeamId;
	/**
	 * 指定到角色
	 */
	@ManyToOne(fetch = FetchType.EAGER, optional = true)
	private Role toRole;
	@Transient
	private Long toRoleId;
	/**
	 * 指定到人员
	 */
	@ManyToOne(fetch = FetchType.EAGER, optional = true)
	private User toUser;

	@Transient
	private Long toUserId;

	/**
	 * 目標leads
	 */
	@ManyToOne(fetch = FetchType.EAGER, optional = true)
	private Leads selLeads;
	@Transient
	private Long selLeadsId;
 
	@ManyToOne(fetch = FetchType.LAZY)
	private User updateUser;

	@Temporal(TemporalType.TIMESTAMP)
	private Date updateTime;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public EUserTodoType getTodoType() {
		return todoType;
	}

	public void setTodoType(EUserTodoType todoType) {
		this.todoType = todoType;
	}

	public EUserTodoStatus getStatus() {
		return status;
	}

	public void setStatus(EUserTodoStatus status) {
		this.status = status;
	}

	public EUserTodoObject getToObject() {
		return toObject;
	}

	public void setToObject(EUserTodoObject toObject) {
		this.toObject = toObject;
	}

	public User getCreateUser() {
		return createUser;
	}

	public void setCreateUser(User createUser) {
		this.createUser = createUser;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getReminderStartTime() {
		return reminderStartTime;
	}

	public void setReminderStartTime(Date reminderStartTime) {
		this.reminderStartTime = reminderStartTime;
	}

	public Date getReminderEndTime() {
		return reminderEndTime;
	}

	public void setReminderEndTime(Date reminderEndTime) {
		this.reminderEndTime = reminderEndTime;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public Department getToDepartment() {
		return toDepartment;
	}

	public void setToDepartment(Department toDepartment) {
		this.toDepartment = toDepartment;
	}

	public Role getToRole() {
		return toRole;
	}

	public void setToRole(Role toRole) {
		this.toRole = toRole;
	}

	public User getToUser() {
		return toUser;
	}

	public void setToUser(User toUser) {
		this.toUser = toUser;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
 
	/**
	 * 根据情况组成ACTION
	 * @return
	 */
	public String prepareAction() {
		return "";
	}
	 

	public Team getToTeam() {
		return toTeam;
	}

	public void setToTeam(Team toTeam) {
		this.toTeam = toTeam;
	}

	public Leads getSelLeads() {
		return selLeads;
	}

	public void setSelLeads(Leads selLeads) {
		this.selLeads = selLeads;
	}

	public Long getToDepartmentId() {
		if (this.toDepartment != null) {
			toDepartmentId = toDepartment.getId();
		}
		return toDepartmentId;
	}

	public void setToDepartmentId(Long toDepartmentId) {
		this.toDepartmentId = toDepartmentId;
	}

	public Long getToTeamId() {
		if (this.toTeam != null) {
			toTeamId = toTeam.getId();
		}
		return toTeamId;
	}

	public void setToTeamId(Long toTeamId) {
		this.toTeamId = toTeamId;
	}

	public Long getToRoleId() {
		if (this.toRole != null) {
			toRoleId = toRole.getId();
		}
		return toRoleId;
	}

	public void setToRoleId(Long toRoleId) {
		this.toRoleId = toRoleId;
	}

	public Long getToUserId() {
		if (this.toUser != null) {
			toUserId = toUser.getId();
		}
		return toUserId;
	}

	public void setToUserId(Long toUserId) {
		this.toUserId = toUserId;
	}

	public Long getSelLeadsId() {
		if (this.selLeads != null) {
			selLeadsId = selLeads.getId();
		}
		return selLeadsId;
	}

	public void setSelLeadsId(Long selLeadsId) {
		this.selLeadsId = selLeadsId;
	}

	public String getToObjectMessage() {
		String cuser = "";
		if (this.createUser != null) {
			cuser = createUser.getName() + " TO ";
		}
		if (toObject != null) {
			if (toObject.equals(EUserTodoObject.MySelf)) {
				return "My Event";
			} else if (toObject.equals(EUserTodoObject.Department)) {
				if (toDepartment != null) {
					return cuser + "Department:" + toDepartment.getName();
				}
			} else if (toObject.equals(EUserTodoObject.Team)) {
				if (toTeam != null) {
					return cuser + "Team:" + toTeam.getName();
				}
			} else if (toObject.equals(EUserTodoObject.Role)) {
				if (toRole != null) {
					return cuser + "Role:" + toRole.getName();
				}
			} else if (toObject.equals(EUserTodoObject.User)) {
				if (toUser != null) {
					return cuser + "User:" + toUser.getName();
				}
			}
		}
		return cuser;
	}

	/**
	 * 是否可以编辑 本人建立的有效的任务可以编辑
	 * 
	 * @return
	 */
	public boolean isCanEdit(Long userid) {
		if (this.createUser == null || userid == null) {
			return false;
		} else {
			if (this.status.equals(EUserTodoStatus.Valid)
					|| this.status.equals(EUserTodoStatus.Expired)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 是否可以删除
	 * 
	 * @return
	 */
	public boolean isCanCancel(Long userid) {
		if (this.createUser == null || userid == null) {
			return false;
		} else {
			if (this.status.equals(EUserTodoStatus.Valid)
					&& createUser.getId().equals(userid)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 是否有快捷键
	 * 
	 * @return
	 */
	public boolean hasAction() {
		if (this.status.equals(EUserTodoStatus.Valid) && this.goAction != null
				&& goAction.trim().length() > 0) {
			return true;
		}
		return false;
	}

	public boolean isCanFinish() {
		if (status.equals(EUserTodoStatus.Valid)
				|| status.equals(EUserTodoStatus.Expired)) {
			return true;
		}
		return false;
	}

	public Date getFinishTime() {
		return finishTime;
	}

	public void setFinishTime(Date finishTime) {
		this.finishTime = finishTime;
	}

	public User getFinishUser() {
		return finishUser;
	}

	public void setFinishUser(User finishUser) {
		this.finishUser = finishUser;
	}

	public String getSelLeadsInfo() {
		if (this.getSelLeads() == null) {
			return "";
		}
		String card = "";
		if (this.getSelLeads().getEliteCard() != null) {
			card = "[" + getSelLeads().getEliteCard() + "]";
		}
		String tel = "";
		if (this.getSelLeads().getTelephone() != null) {
			tel = "[TEL:" + getSelLeads().getTelephone() + "]";
		}
		return this.getSelLeads().getName() + card + tel;
	}

	public String getFinishType() {
		if (status.equals(EUserTodoStatus.Finished)
				|| status.equals(EUserTodoStatus.Cancel)) {
			return status.toString();
		} else {
			return "";
		}
	}

	public String getGoAction() {
		return goAction;
	}

	public void setGoAction(String goAction) {
		this.goAction = goAction;
	}

	public User getUpdateUser() {
		return updateUser;
	}

	public void setUpdateUser(User updateUser) {
		this.updateUser = updateUser;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
}
